package com.demo.dao.impl;


import com.demo.dao.UserDao;
import com.demo.entity.User;
import com.demo.utils.C3P0Utiles;
import com.demo.utils.TransactionMnanager;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@Repository
public class UserDaoImpl implements UserDao {
    @Override
    public void addUser2(User user) {
        Connection conn=null;
        PreparedStatement ps=null;
        try {
            conn = TransactionMnanager.getConn();
            ps = conn.prepareStatement("insert into user values(null,?,?)");
            ps.setString(1,user.getName());
            ps.setInt(2,user.getAge());
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            C3P0Utiles.close(null,ps,null);
        }
    }

    @Override
    public void delUser2(int id) {
        Connection conn=null;
        PreparedStatement ps=null;
        try {
            conn=TransactionMnanager.getConn();
            ps = conn.prepareStatement("delete from user where id=?");
            ps.setInt(1,id);
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            C3P0Utiles.close(null,ps,null);
        }
    }
}

